import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:cbec_app/pages/onlineCliet/udesk_webView_controller.dart';
import 'package:cbec_app/components/base/base_page.dart';
import 'package:cbec_app/components/base/base_nav_bar.dart';

class UdeskWebViewPage extends GetView<UdeskWebViewController> {
  const UdeskWebViewPage({super.key});

  @override
  Widget build(BuildContext context) {
    return GetBuilder<UdeskWebViewController>(builder: (controller) {
      return BasePage(
        title: "客服",
        iconType: IconType.back,
        bodyWidget: Obx(() => Stack(
              children: [
                InAppWebView(
                  initialUrlRequest: URLRequest(
                    url: WebUri(controller.urlString), // 使用 WebUri 构造函数
                  ),
                  initialSettings: InAppWebViewSettings(
                    javaScriptEnabled: true,
                    useOnDownloadStart: true,
                    mediaPlaybackRequiresUserGesture: false, // 允许自动播放媒体
                  ),
                  onWebViewCreated: (webViewController) {
                    controller.webController = webViewController;
                  },
                  onLoadStart: (ctr, url) {
                    controller.isLoading.value = true; // 页面开始加载时
                  },
                  onLoadStop: (ctr, url) async {
                    controller.isLoading.value = false; // 页面加载完成时
                  },
                  onDownloadStartRequest: (controller, url) {
                    print("下载请求: $url");
                    // 在这里处理文件下载请求
                  },
                  // onPermissionRequest: (controller, request) async {
                  //   return null;
                  // },
                ),
                if (controller.isLoading.value) const Center(child: CircularProgressIndicator()), // 显示加载指示器
              ],
            )),
      );
    });
  }
}
